package nl.runnable.solr.xslt.client;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.util.Assert;

/**
 * Adapter for providing access to XML content to {@link SolrXmlIndexService} operations.
 * <p>
 * Subclasses must implement {@link #getContentAsStream()} to provide an {@link InputStream} to the raw XML content.
 * Clients are responsible for closing this {@link InputStream}.
 * 
 * @author Laurens Fridael
 */
public abstract class XmlContent {

	private final String mimeType;

	private final String encoding;

	protected XmlContent(final String mimeType, final String encoding) {
		Assert.hasText(mimeType, "MIME type cannot be empty.");
		this.mimeType = mimeType;
		this.encoding = encoding;
	}

	/**
	 * Obtains the MIME type. Typically <code>text/xml</code>, but clients or subclasses may provide a more specific
	 * MIME type.
	 * 
	 * @return
	 */
	public String getMimeType() {
		return mimeType;
	}

	/**
	 * The content's character encoding. Note that it is actually recommended that the XML specifies the encoding in its
	 * prolog, so client may choose to ignore the value provided.
	 * 
	 * @return The encoding or null if unknown.
	 */
	public String getEncoding() {
		return encoding;
	}

	/**
	 * Obtains an {@link InputStream} to the physical XML content. Clients are responsible for closing the stream.
	 * 
	 * @return
	 * @throws IOException
	 */
	public abstract InputStream getContentAsStream() throws IOException;

}
